package com.sysd.mail.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * Project: IntelliJ IDEA
 *
 * @Description:
 * @Auther: bsh
 * @DataTime: 2022/4/8 14:37
 * @Company: 时度
 */
public class DateUtil {
    //设置日期格式
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String LOCAL_DATE_FORMAT = "yyyy/MM/dd";

    /**
     * 获取String类型的LocalDate
     *
     * @return String current time
     */
    public static String getCurrentDateTimeFormat() {
        return DateTimeFormatter.ofPattern(DATE_TIME_FORMAT).format(DateUtil.getCurrentDT());
    }

    /**
     * 获取Local_Date_Format格式
     *
     * @param date
     * @return
     */
    public static String getLocalDateFormat(LocalDate date) {
        return DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT).format(date);
    }

    /**
     * 字符串转换时间
     *
     * @param dateString
     * @return
     */
    public static LocalDate stringToLocalDate(String dateString) {
        if (dateString == null || dateString.length() == 0) {
            return null;
        }
        return LocalDate.parse(dateString);
    }

    /**
     * 计算时间天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getDays(LocalDate startDate, LocalDate endDate) {
        return startDate.until(endDate, ChronoUnit.DAYS) + 1;
    }

    /**
     * 获取当前时间年月日
     *
     * @return
     */
    public static String getCurrentDate() {
        SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_FORMAT);
        return df.format(new Date());
    }


    /**
     * 获取当前时间年月日时分秒
     *
     * @return
     */
    public static String getCurrentDateTime() {
        SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT);
        return df.format(new Date());
    }

    /**
     * 获取当前时间LocalDateTime
     *
     * @return
     */
    public static LocalDateTime getCurrentDT() {
        return LocalDateTime.now();
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static LocalDate getCurrentD() {
        return LocalDate.now();
    }

    /**
     * 获取当前周
     *
     * @return
     */
    public static Integer getCurrentWeek() {
        return DateUtil.getCurrentDT().getDayOfWeek().getValue();
    }

    /**
     * 获取这个时间的月份
     *
     * @param date
     * @return
     */
    public static Integer getCurrentMonth(LocalDate date) {
        if (date == null) {
            return null;
        }
        return date.getMonth().getValue();
    }

    /**
     * 获取本周周一
     *
     * @return
     */
    public static LocalDate getCurrentWeekMonday() {
        return DateUtil.getCurrentD().with(DayOfWeek.MONDAY);
    }

    /**
     * 获取本周周日
     *
     * @return
     */
    public static LocalDate getCurrentWeekSunday() {
        return DateUtil.getCurrentD().with(DayOfWeek.SUNDAY);
    }

    /**
     * 获取每月的第一天
     *
     * @return
     */
    public static LocalDate getCurrentMonthFirstDay() {
        return DateUtil.getCurrentD().with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 获取每月的最后一天
     *
     * @return
     */
    public static LocalDate getCurrentMonthLastDay() {
        return DateUtil.getCurrentD().with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 获取某日的周一
     *
     * @param localDate
     * @return
     */
    public static LocalDate getMonthFirstDay(LocalDate localDate) {
        return localDate.with(DayOfWeek.MONDAY);
    }

    /**
     * 获取某日的周日
     *
     * @param localDate
     * @return
     */
    public static LocalDate getMonthLastDay(LocalDate localDate) {
        return localDate.with(DayOfWeek.SUNDAY);
    }

    /**
     * 格式转换
     *
     * @param date
     * @return
     */
    public static LocalDateTime DateToLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * 获取本年第一天
     *
     * @return
     */
    public static LocalDate getCurrentFirstDay() {
        return DateUtil.getCurrentD().with(TemporalAdjusters.firstDayOfYear());
    }

    /**
     * 获取本年最后一天
     *
     * @return
     */
    public static LocalDate getCurrentLastDay() {
        return DateUtil.getCurrentD().with(TemporalAdjusters.lastDayOfYear());
    }

    /**
     * 获取当前年份
     *
     * @return
     */
    public static String getCurrentYear() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy");
        return df.format(new Date());
    }

    /**
     * 获取当前月份
     *
     * @return
     */
    public static String getCurrentMonth() {
        SimpleDateFormat df = new SimpleDateFormat("MM");
        return df.format(new Date());
    }

    /**
     * 获取当前月份
     *
     * @param date
     * @return
     */
    public static String getCurrentMonth(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("MM");
        return df.format(date);
    }

    /**
     * 自定义返回时间格式
     *
     * @param datestr
     * @return
     */
    public static String getCurrentDateTime(String datestr) {
        SimpleDateFormat df = new SimpleDateFormat(datestr);
        return df.format(new Date());
    }

    /**
     * 日期转换字符串
     *
     * @param date
     * @return
     */
    public static String DateToString(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_FORMAT);
        return df.format(date);
    }

    /**
     * 日期转换字符串
     *
     * @param date
     * @return
     */
    public static String DateTimeToString(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT);
        return df.format(date);
    }

    /**
     * 自定义转换格式
     *
     * @param strdate
     * @param dateformat
     * @return
     */
    public static Date StringToDate(String strdate, String dateformat) {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(dateformat);
        try {
            date = df.parse(strdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 字符串转换成日期
     *
     * @param strdate
     * @return
     */
    public static Date StringToDateTime(String strdate) {
        if (strdate.isEmpty()) {
            return null;
        }
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT);
        try {
            date = df.parse(strdate);
        } catch (Exception e) {
            date = DateUtil.StringToDate(strdate, "yyyy-MM-dd");
        }
        return date;
    }
}
